<?php

namespace App\Console\Commands;

use App\Http\Models\Mysql\SsyProcessingListTable;
use App\Http\Models\Mysql\SsyPushOmsJavaLogTable;
use App\Http\Repository\OmsRepository;
use Illuminate\Console\Command;

class updateJyPno extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:updateJyPno {--repeat=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新采购通知单号(PNO)--巨益';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $repeat = $this->option('repeat') ?? 0;
        //首次获取
        if(empty($repeat)){
            $this->getData();
        }else{
            //取消后的加工单重新获取
            $this->getRepeat();
        }
    }

    //首次获取采购通知单 PNO
    public function getData()
    {
        $field = ["processing_code"];
        $processingList = (new SsyProcessingListTable())->where('status', 1)->where('is_oms', 1)->whereNotNull('jy_number')->whereNull('jy_purchase_number')
            ->groupBy($field)->get($field)->toArray();
        $res = OmsRepository::searchPno(implode(",", array_column($processingList, 'processing_code')));
        $pnoMap = [];
        foreach ($res['order'] as $item){
            if($item['status'] == "CANCEL"){
                continue;
            }
            if($item['fromCode'] ?? "" && $item['purchaseNoticeOrderCode']){
                $pnoMap[$item['fromCode']] = $item['purchaseNoticeOrderCode'];
            }
        }
        foreach ($processingList as $item) {
            if(isset($pnoMap[$item['processing_code']])){
                echo "正在处理: ".$item['processing_code']." PNO: ".$pnoMap[$item['processing_code']]."\n";
                (new SsyProcessingListTable())->updateDataWhere(
                    ['processing_code' => $item['processing_code']],
                    ['jy_purchase_number' => $pnoMap[$item['processing_code']]]
                );
            }
        }
    }

    //取消过的加工单 重新获取 PNO
    public function getRepeat()
    {
        $date = date('Y-m-d H:i:s', strtotime('- 12 hours'));
        $cancelLog = (new SsyPushOmsJavaLogTable())->where('title', '取消采购单')->where('data_status', 0)->where('createtime', '>', $date)->get()->toArray();
        foreach ($cancelLog as $logItem){
            $dataList = json_decode($logItem['datalist'], true);
            $processingObj = (new SsyProcessingListTable())->where('processing_code', $dataList['orderCode'])->where('status', 1)->where('is_oms', 1)->where('f_status', 0)
                ->first();
            if(empty($processingObj)){
                continue;
            }
            $res = OmsRepository::searchPno($processingObj->processing_code);
            foreach ($res['order'] as $item){
                if($item['status'] == 'CANCEL'){
                    continue;
                }
                (new SsyProcessingListTable())->updateDataWhere(
                    ['processing_code' => $processingObj->processing_code],
                    ['jy_purchase_number' => $item['purchaseNoticeOrderCode']]
                );
                (new SsyPushOmsJavaLogTable())->updateData($logItem['id'], ['data_status' => 1]);
            }
        }
    }
}
